package com.sixth.system.service.impl;/**
 * @author 633yh
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixth.core.entity.LoginInfo;
import com.sixth.core.vo.R;
import com.sixth.system.mapper.LoginInfoMapper;
import com.sixth.system.service.LoginInfoService;
import com.sixth.system.vo.LoginInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * @author     ：Stu YH
 * @date       ：Created in 2024/8/18 19:16
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class LoginInfoServiceImpl implements LoginInfoService {
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public R<IPage<LoginInfo>> selectByPage(LoginInfoVo loginInfoVo) {
        IPage<LoginInfo> page = new Page<>(loginInfoVo.getPageNum(), loginInfoVo.getPageSize());
        QueryWrapper<LoginInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(loginInfoVo.getUserName())){
            wrapper.like("user_name",loginInfoVo.getUserName());
        }
        if (StringUtils.hasText(loginInfoVo.getOs())){
            wrapper.eq("os",loginInfoVo.getOs());
        }
        if (StringUtils.hasText(loginInfoVo.getLoginAccount())){
            wrapper.like("login_account",loginInfoVo.getLoginAccount());
        }
        if (StringUtils.hasText(loginInfoVo.getLoginStatus())){
            wrapper.like("login_status",loginInfoVo.getLoginStatus());
        }
        System.out.println("~~~~~~"+StringUtils.hasText(loginInfoVo.getLoginStatus()));// true
        if ((StringUtils.hasText(loginInfoVo.getLoginType()))){
            wrapper.eq("login_type",loginInfoVo.getLoginType());
        }
        if (StringUtils.hasText(loginInfoVo.getIpAddr())){
            wrapper.eq("ip_addr",loginInfoVo.getIpAddr());
        }
        if (loginInfoVo.getDateRange()!=null&&loginInfoVo.getDateRange().size()==2){
            wrapper.between("login_time",loginInfoVo.getDateRange().get(0),loginInfoVo.getDateRange().get(1));
        }
        IPage<LoginInfo> loginInfoIPage = loginInfoMapper.selectPage(page, wrapper);
        return R.ok(loginInfoIPage);
    }

    @Override
    public R removeByIds(Long[] ids) {
        loginInfoMapper.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    @Override
    public boolean deleteAll() {
        loginInfoMapper.deleteAll();
        return true;
    }


    public R save(LoginInfo loginInfo) {
        int i = loginInfoMapper.insert(loginInfo);
        return i>0?R.ok():R.error("日志添加失败");
    }


}